package cn.edu.uestc.topo;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.json.JSONArray;
import org.json.JSONObject;

public class TunnelInfo {

    private UUID id = null;
    private String name = null;
    private UUID nodeId = null;
    private UUID linkId = null;
    private UUID portId = null;
    private String ip = null;

    /**
     * 4 - Gold
     * 3 - Silver
     * 2 - Iron
     * 1 - Copper
     */
    private int priority = 1;

    private String srcIP = null;
    private String dstIP = null;
    private long bandwidthRequest;
    private int delayRequest;
    private int hopRequest = 24;
    private UUID tunnelPathId = null;
    private List<UUID> passNodeList = null;
    private List<UUID> avoidNodeList = null;
    private List<UUID> passLinkList = null;
    private List<UUID> avoidLinkList = null;
    private String passSequence = null;

    public TunnelInfo() {

    }

    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }

    public int getHopRequest() {
        return hopRequest;
    }

    public void setHopRequest(int hopRequest) {
        this.hopRequest = hopRequest;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public UUID getNodeId() {
        return nodeId;
    }

    public void setNodeId(UUID nodeId) {
        this.nodeId = nodeId;
    }

    public UUID getLinkId() {
        return linkId;
    }

    public void setLinkId(UUID linkId) {
        this.linkId = linkId;
    }

    public UUID getPortId() {
        return portId;
    }

    public void setPortId(UUID portId) {
        this.portId = portId;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public String getSrcIP() {
        return srcIP;
    }

    public void setSrcIP(String srcIP) {
        this.srcIP = srcIP;
    }

    public String getDstIP() {
        return dstIP;
    }

    public void setDstIP(String dstIP) {
        this.dstIP = dstIP;
    }

    public long getBandwidthRequest() {
        return bandwidthRequest;
    }

    public void setBandwidthRequest(long bandwidthRequest) {
        this.bandwidthRequest = bandwidthRequest;
    }

    public int getDelayRequest() {
        return delayRequest;
    }

    public void setDelayRequest(int delayRequest) {
        this.delayRequest = delayRequest;
    }

    public UUID getTunnelPathId() {
        return tunnelPathId;
    }

    public void setTunnelPathId(UUID tunnelPathId) {
        this.tunnelPathId = tunnelPathId;
    }

    public List<UUID> getPassNodeList() {
        return passNodeList;
    }

    public void setPassNodeList(List<UUID> passNodeList) {
        this.passNodeList = passNodeList;
    }

    public List<UUID> getAvoidNodeList() {
        return avoidNodeList;
    }

    public void setAvoidNodeList(List<UUID> avoidNodeList) {
        this.avoidNodeList = avoidNodeList;
    }

    public List<UUID> getPassLinkList() {
        return passLinkList;
    }

    public void setPassLinkList(List<UUID> passLinkList) {
        this.passLinkList = passLinkList;
    }

    public List<UUID> getAvoidLinkList() {
        return avoidLinkList;
    }

    public void setAvoidLinkList(List<UUID> avoidLinkList) {
        this.avoidLinkList = avoidLinkList;
    }

    public String getPassSequence() {
        return passSequence;
    }

    public void setPassSequence(String passSequence) {
        this.passSequence = passSequence;
    }

    public void readFromJson(JSONObject jsonObject) {

        try {
            this.id = UUID.fromString(jsonObject.getString("id"));
            this.name = jsonObject.getString("name");
            this.nodeId = UUID.fromString(jsonObject.getString("nodeId"));
            this.linkId = UUID.fromString(jsonObject.getString("linkId"));
            this.portId = UUID.fromString(jsonObject.getString("portId"));
            this.ip = jsonObject.getString("ip");
            this.priority = jsonObject.getInt("priority");
            this.srcIP = jsonObject.getString("srcIP");
            this.dstIP = jsonObject.getString("dstIP");
            this.bandwidthRequest = jsonObject.getLong("bandwidthRequest");
            this.delayRequest = jsonObject.getInt("delayRequest");
            this.hopRequest = jsonObject.getInt("hopRequest");

            this.passNodeList = new ArrayList<>();
            JSONArray passNodeArray = jsonObject.getJSONArray("passNodeList");
            for(int i = 0; i < passNodeArray.length(); i++) {
                this.passNodeList.add(UUID.fromString(passNodeArray.getString(i)));
            }

            this.avoidNodeList = new ArrayList<>();
            JSONArray avoidNodeArray = jsonObject.getJSONArray("avoidNodeList");
            for(int i = 0; i < avoidNodeArray.length(); i++) {
                this.avoidNodeList.add(UUID.fromString(avoidNodeArray.getString(i)));
            }

            this.passLinkList = new ArrayList<>();
            JSONArray  passLinkArray = jsonObject.getJSONArray("passLinkList");
            for (int i = 0; i < passLinkArray.length(); i++) {
                this.passLinkList.add(UUID.fromString(passLinkArray.getString(i)));
            }

            this.avoidLinkList = new ArrayList<>();
            JSONArray avoidLinkArray = jsonObject.getJSONArray("avoidLinkList");
            for(int i = 0; i < avoidLinkArray.length(); i++) {
                this.avoidLinkList.add(UUID.fromString(avoidLinkArray.getString(i)));
            }

            this.passSequence = jsonObject.getString("passSequence");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public JSONObject writeToJson() {
        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("id", this.id.toString());
            jsonObject.put("name", this.name);
            jsonObject.put("nodeId", this.nodeId.toString());
            jsonObject.put("portId", this.portId.toString());
            jsonObject.put("linkId", this.linkId.toString());
            jsonObject.put("ip", this.ip);
            jsonObject.put("priority", this.priority);
            jsonObject.put("srcIP", this.srcIP);
            jsonObject.put("dstIP", this.dstIP);
            jsonObject.put("bandwidthRequest", this.bandwidthRequest);
            jsonObject.put("delayRequest", this.delayRequest);
            jsonObject.put("hopRequest", this.hopRequest);

            JSONArray passNodeArray = new JSONArray(this.passNodeList);
            jsonObject.put("passNodeList", passNodeArray);

            JSONArray avoidNodeArray = new JSONArray(this.avoidNodeList);
            jsonObject.put("avoidNodeList", avoidNodeArray);

            JSONArray passLinkArray = new JSONArray(this.passLinkList);
            jsonObject.put("passLinkList", passLinkArray);

            JSONArray avoidLinkArray = new JSONArray(this.avoidLinkList);
            jsonObject.put("avoidLinkList", avoidLinkArray);

            jsonObject.put("passSequence", this.passSequence);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        String stringContent = null;
        try {
            stringContent = this.writeToJson().toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return stringContent;
    }
}
